package cn.com.surker.system.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.oa.dao.OaEnclosureDao;
import cn.com.surker.oa.entity.OaEnclosure;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.service.OaEnclosureService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 附件信息 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-10-05
 */
@Service
public class OaEnclosureServiceImpl extends ServiceImpl<OaEnclosureDao, OaEnclosure> implements OaEnclosureService {

	@Resource
	private OaEnclosureDao mapper;


	/**
	 * 根据条件查询所有的数据
	 *
	 * @param dto
	 * @return
	 */
	public List<OaEnclosure> findAll(OaEnclosure dto) {
		QueryWrapper<OaEnclosure> query = new QueryWrapper();
		query.eq("status",StateEnum.ENABLED.getCode());
		if(dto != null){
			if(dto.getBizId() != null){
				query.eq("biz_id",dto.getBizId());
			}
			if(dto.getUid() != null){
				query.eq("uid",dto.getUid());
			}
			if(StringUtils.isNotBlank(dto.getTableName())){
				query.eq("table_name",dto.getTableName());
			}
		}
		List<OaEnclosure> list = mapper.selectList(query);
		return list;
	}

	/**
	 * 新增数据
	 *
	 * @param dto page
	 * @return
	 */
	public RestResultDto saveInit(OaEnclosure dto) {
		int saveStatus = mapper.insert(dto);
		if (saveStatus > 0) {
			return RestResultDto.success("保存成功!");
		} else {
			return RestResultDto.success("保存失败!");
		}

	}

	/**
	 * 更新数据
	 *
	 * @param dto page
	 * @return
	 */
	public RestResultDto update(OaEnclosure dto) {

		int saveStatus = mapper.updateById(dto);
		if (saveStatus > 0) {
			return RestResultDto.success("保存成功!");
		} else {
			return RestResultDto.success("保存失败!");
		}

	}

	@Override
	public void delete(Long id) {
		if (id != null) {
			OaEnclosure oaEnclosure = new OaEnclosure();
			oaEnclosure.setId(id);
			oaEnclosure.setStatus(StateEnum.DISABLED.getCode());
			mapper.updateById(oaEnclosure);
		}
	}

	public OaEnclosure selectById(Long id) {
		if (id == null) {
			return null;
		}
		QueryWrapper<OaEnclosure> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id);
		queryWrapper.eq("status", StateEnum.ENABLED.getCode());
		OaEnclosure oaEnclosure = mapper.selectOne(queryWrapper);
		return oaEnclosure;
	}
}